﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Hawaii.Services.Client;
using Hawaii.Services.Client.Relay;
using System.Text;
using Convo.POCOs;

namespace Convo
{
    public partial class ChatPage : PhoneApplicationPage
    {
        private String name;
        private String id;

        private ConvId ConvContext
        {
            get { return (App.Current as App).ConvContext; }
        }

        public ChatPage()
        {
            InitializeComponent();
            (App.Current as App).ChatRef = this;
        }


        private delegate void DisplayMessagesDelegate();
        private RelayContext RelayContext
        {
            get { return ((App)App.Current).RelayContext; }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            string messageContent = textBox1.Text;
            if (!string.IsNullOrEmpty(messageContent))
            {
                byte[] message = Encoding.Unicode.GetBytes(messageContent);
                RelayService.SendMessageAsync(HawaiiClient.HawaiiApplicationId,
                 this.RelayContext.Endpoint,
                 this.RelayContext.Endpoint.RegistrationId,
                 message,
                 this.OnCompleteSendMessage);
            }
        }

        private void OnCompleteSendMessage(MessagingResult result)
        {
            if (result.Status == Status.Success)
            {
                // display message in conversation gui
            }
        }



        private void DisplayMessages()
        {
            foreach (Message message in this.RelayContext.Messages)
            {
                StringBuilder text = new StringBuilder();
               
                text.Append(Encoding.Unicode.GetString(message.Body, 0, message.Body.Length));
                text.AppendLine();
                textBox2.Text = text.ToString();
            }
        }
    }
}

